<?php


namespace app\api\model;


use app\common\library\aliyunsms\alidayu;
use app\common\model\BaseModel;

const EVENT_TYPE_UNDER_VOLT = 1;
const EVENT_TYPE_OFFLINE = 2;
const EVENT_TYPE_BIKE_SUSPECTED_STOLEN = 3;
const EVENT_TYPE_MISSING_BIKE_SCANED = 4;
const EVENT_TYPE_EX_OFF_BATTERY = 5;

class EventNoticeReceiverModel extends BaseModel
{
    protected $name = 'event_notice_receiver';

    static function instance()
    {
        static $_thiz = null;
        if (empty($_thiz)) {
            $_thiz = new self();
        }
        return $_thiz;
    }

    var $_recv_list = [];
    var $alert_type_map = [
        EVENT_TYPE_UNDER_VOLT => '车辆欠压',
        EVENT_TYPE_OFFLINE => '车辆离线',
        EVENT_TYPE_BIKE_SUSPECTED_STOLEN => '车辆疑似被盗',
        EVENT_TYPE_MISSING_BIKE_SCANED => '失联车辆用户扫码',
        EVENT_TYPE_EX_OFF_BATTERY => '车辆电池异常空置',
    ];

    function set_recvrer($city_id, $event_type, $receivers)
    {
        $info = [
            'receivers' => $receivers,
        ];
        if (!isset($this->alert_type_map[$event_type]))
            return;

        \ApiOper::instance()->append_updater_info($info);

        $where = [
            'city_id' => $city_id,
            'event_type' => $event_type,
        ];

        $ef = self::update($info, $where);


        if ($ef == 0) {
            $info = array_merge($info, $where);
            $info['event_name'] = $this->alert_type_map[$event_type];
            self::create($info,true);
        }
    }

    function query_list($city_id)
    {
        $selectlist = [
            'event_type' => 'int',
            'event_name' => 'string',
            'receivers' => 'array',
            'update_time' => 'string',
            'updater_id' => 'string',
            'updater_name' => 'string',
        ];

        $beans = [
            'city_id'=>$city_id
        ];

        $result = $this->where($beans)->field(array_keys($selectlist))->select();

        $l = [];
        foreach ($result as $row) {
            $info = \common::sql_row($selectlist, $row);
            $l[$info['event_type']] = $info;
        }

        foreach ($this->alert_type_map as $type => $name) {
            if (!isset($l[$type])) {
                $l[$type] = [
                    'event_type' => $type,
                    'event_name' => $name,
                    'receivers' => [],
                    'update_time' => null,
                    'updater_id' => null,
                    'updater_name' => null,
                ];
            }
        }
        $this->_recv_list = $l;
        return $l;
    }

    function get_receiver_phone_list($city_id, $type)
    {
        if (empty($this->_recv_list[$city_id])) {
            $this->_recv_list[$city_id] = $this->query_list($city_id);
        }

        $s = [];
        if (isset($this->_recv_list[$city_id][$type])) {
            foreach ($this->_recv_list[$city_id][$type]['receivers'] as $rv) {
                $s[] = $rv['phone'];
            }
        }
        return $s;
    }

    function send_sms_miss_bike_scaned($city_id, $bike_sn)
    {
        $phones = $this->get_receiver_phone_list($city_id, EVENT_TYPE_MISSING_BIKE_SCANED);
        $city = "";
        $city_info = (new CityModel())->query_info($city_id);
        if (!empty($city_info)) {
            $city = $city_info['name'];
        }
        foreach ($phones as $phone) {
            $data = [
                'city' => $city,
                'name' => \ApiOper::instance()->userinfo()['name'],
                'phone' => \ApiOper::instance()->userinfo()['phone'],
                'sn' => $bike_sn,
                'date' => date('m-d H:i'),
            ];
            (new alidayu())->send_sms_alert($phone, ALERT_SMS_TPL_MISS_BIKE_SCANED_V2, $data);
        }
    }

    function send_sms_bike_stolen($city_id, $bike_sn, $speed)
    {
        $speed = "{$speed}km/h";
        $phones = $this->get_receiver_phone_list($city_id, EVENT_TYPE_BIKE_SUSPECTED_STOLEN);
        $city = "多多e行";
        $city_info = (new CityModel())->query_info($city_id);
        if (!empty($city_info)) {
            $city = $city_info['name'];
        }
        $bike_info = BikeModel::instance()->get_info_by_sn($bike_sn);
        foreach ($phones as $phone) {
            $data = [
                'city' => $city,
                'sn' => $bike_sn,
                'date' => date('m-d H:i'),
                'speed' => $speed,
                'addr' => $bike_info['address'],
            ];
            (new alidayu())->send_sms_alert($phone, ALEERT_SMS_TPL_BIKE_STOLEN_V2, $data);
        }
    }

    function send_sms_bike_under_volt($city_id, $bike_sn)
    {
        $phones = $this->get_receiver_phone_list($city_id, EVENT_TYPE_UNDER_VOLT);
        $city = "多多e行";
        $city_info = (new CityModel())->query_info($city_id);
        if (!empty($city_info)) {
            $city = $city_info['name'];
        }
        foreach ($phones as $phone) {
            $data = [
                'city' => $city,
                'sn' => $bike_sn,
                'date' => date('m-d H:i'),
            ];
            (new alidayu())->send_sms_alert($phone, ALERT_SMS_TPL_BIKE_UNDER_VOLT_V2, $data);
        }
    }

    function send_sms_bike_offline($city_id, $bike_sn)
    {
        $phones = $this->get_receiver_phone_list($city_id, EVENT_TYPE_OFFLINE);
        $city = "多多e行";
        $city_info = (new CityModel())->query_info($city_id);
        if (!empty($city_info)) {
            $city = $city_info['name'];
        }
        foreach ($phones as $phone) {
            $data = [
                'city' => $city,
                'sn' => $bike_sn,
                'date' => date('m-d H:i'),
            ];
            (new alidayu())->send_sms_alert($phone, ALERT_SMS_TPL_BIKE_OFFLINE_V2, $data);
        }
    }

    function send_sms_bike_ex_off_battery($city_id, $bike_sn)
    {
        $phones = $this->get_receiver_phone_list($city_id, EVENT_TYPE_EX_OFF_BATTERY);
        $city = "多多e行";
        $city_info = (new CityModel())->query_info($city_id);
        if (!empty($city_info)) {
            $city = $city_info['name'];
        }
        foreach ($phones as $phone) {

            $data = [
                'city' => $city,
                'sn' => $bike_sn,
                'date' => date('m-d H:i'),
            ];
            (new alidayu())->send_sms_alert($phone, ALERT_SMS_TPL_BIKE_EX_OFF_BATTERY_V2, $data);
        }
    }

    function send_vmsg_bike_stolen($city_id, $sn, $bus_state, $speed, $address)
    {
        require_once APP_PATH . "/aliyun-dyvms-php-sdk-lite/demo/singleCallByTts.php";
        $phones = $this->get_receiver_phone_list($city_id, EVENT_TYPE_BIKE_SUSPECTED_STOLEN);
        $city = "多多e行";
        $city_info =(new CityModel())->query_info($city_id);
        if (!empty($city_info)) {
            $city = $city_info['name'];
        }

        foreach ($phones as $phone) {
            $ctx = new Aliyun\DySDKLite\Vms\Demo\SingleCallByTtsParams();
            $ctx->to_no = $phone;
            $ctx->tpl_params = [
                'city' => $city,
                'sn' => $sn,
//                'bus_state' => $bus_state,
//                'speed' => $speed,
//                'address' => $address,
            ];
            $ctx->tts_code = 'TTS_198365264';

            $result = Aliyun\DySDKLite\Vms\Demo\singleCallByTts($ctx);
            Log::debug("bike stolen vmsg:{$phone},{$result}");
        }
    }

    function send_vmsg_bike_battery_off($city_id, $sn, $bus_state, $address)
    {
        require_once APP_PATH . "/aliyun-dyvms-php-sdk-lite/demo/singleCallByTts.php";
        $phones = $this->get_receiver_phone_list($city_id, EVENT_TYPE_BIKE_SUSPECTED_STOLEN);
        $city = "多多e行";
        $city_info = (new CityModel())->query_info($city_id);
        if (!empty($city_info)) {
            $city = $city_info['name'];
        }

        foreach ($phones as $phone) {
            $ctx = new Aliyun\DySDKLite\Vms\Demo\SingleCallByTtsParams();
            $ctx->to_no = $phone;
            $ctx->tpl_params = [
                'city' => $city,
                'sn' => $sn,
                'bus_state' => $bus_state,
                'address' => $address,
            ];
            $ctx->tts_code = 'TTS_198435140';

            $result = Aliyun\DySDKLite\Vms\Demo\singleCallByTts($ctx);

            Log::debug("bike stolen vmsg:{$phone},{$result}");
        }
    }
}